"use strict";

const Controller = require("egg").Controller;

const dayjs = require("dayjs");

const OSS = require("ali-oss");

const OSSpage = new OSS({
  region: "oss-cn-beijing",
  accessKeyId: "LTAI5tPxLdFQbT9xMjGv6X1z",
  accessKeySecret: "Jcm5PBbnnJtYSilCltvYYPJvaOJSGD",
  bucket: "1908abucket-dsx",
});

class UploadController extends Controller {
  async index() {
    const { ctx, app } = this;

    const file = ctx.request.files[0];

    const filename = new Date().getTime() + file.filename;

    const data = await OSSpage.put(filename, file.filepath);

    await app.mysql.insert("file", {
      filename,
      filetype: file.mime,
      createtime: dayjs(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss"),
      url: data.url,
    });

    ctx.body = {
      code: 1,
      url: data.url,
    };
  }
}

module.exports = UploadController;
